package com.daily.pump.http;

import android.text.TextUtils;

import com.daily.pump.activity.MainActivity;
import com.daily.pump.utils.StringUtils;

public class WifiSocketManger {

    public static final String HOSTNAME = "192.168.4.1";
    public static final int PORT = 5000;
    //密码最大长度
    public static final int MAX_LENGHT = 8;
    private static WifiSocketManger mInstance;

    private WifiSocketClient mWifiSocketClient;

    private OnWiFiSocketListener mOnWiFiSocketListener;

    public static WifiSocketManger getInstance() {
        synchronized (WifiSocketManger.class) {
            if (mInstance == null) {
                mInstance = new WifiSocketManger();
            }
            return mInstance;
        }
    }

    public WifiSocketManger() {
        if (mWifiSocketClient == null) {
            mWifiSocketClient = new WifiSocketClient();
        }
    }

    public void connnect(OnWiFiSocketListener listener) {
        this.mOnWiFiSocketListener = listener;
        if (mWifiSocketClient == null) {
            mWifiSocketClient = new WifiSocketClient();
        }
        mWifiSocketClient.setOnMessageReceListener(new WifiSocketClient.OnMessageReceListener() {
            @Override
            public void onMessageRece(int code, String message) {
                float compressValue = 0f;
                if (code == WifiSocketClient.SEND_RESPONSE) {
                    if (message.indexOf("PAOK") != -1) {
                        if (mOnWiFiSocketListener != null) {
                            mOnWiFiSocketListener.loginSucc();
                        }
                    } else if (message.indexOf("PANG") != -1) {
                        if (mOnWiFiSocketListener != null) {
                            mOnWiFiSocketListener.loginFail();
                        }
                    } else if (message.indexOf("PBOK") != -1) {
                        if (mOnWiFiSocketListener != null) {
                            mOnWiFiSocketListener.modifyPwdSucc();
                        }
                    } else if (message.indexOf("PBNG") != -1) {
                        if (mOnWiFiSocketListener != null) {
                            mOnWiFiSocketListener.modifyPwdFail();
                        }
                    } else {
                        compressValue = StringUtils.getFormatCompressValue(message);
                        if (mOnWiFiSocketListener != null) {
                            mOnWiFiSocketListener.onRespnse(code, compressValue);
                        }
                    }
                } else {
                    if (mOnWiFiSocketListener != null) {
                        mOnWiFiSocketListener.onRespnse(code, compressValue);
                    }
                }
            }
        });
        mWifiSocketClient.connect(HOSTNAME, PORT);
    }

    public void getCompressValue(int value) {
        value = (int) (value * MainActivity.PERCENT);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("TF");
        if (value < 10) {
            stringBuffer.append("00").append(value);
        } else if (value < 100) {
            stringBuffer.append("0").append(value);
        } else {
            stringBuffer.append(value);
        }
        stringBuffer.append("0F");
        if (mWifiSocketClient != null) {
            mWifiSocketClient.TCPSend(stringBuffer.toString());
        }
    }

    /**
     * Desc:充气键按下
     * <p>
     * Author: [李豫]
     * Date: 2020-09-02
     */
    public void onInflatable() {
        if (mWifiSocketClient != null) {
            mWifiSocketClient.TCPSend("TF1001F");
        }
    }

    /**
     * Desc:停止充气键按下
     * <p>
     * Author: [李豫]
     * Date: 2020-09-02
     */
    public void onStop() {
        if (mWifiSocketClient != null) {
            mWifiSocketClient.TCPSend("TF1007F");
        }
    }

    /**
     * Desc:S键按下
     * <p>
     * Author: [李豫]
     * Date: 2020-09-02
     */
    public void onSPress(int softValue) {
        softValue = (int) (softValue * MainActivity.PERCENT);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("TF");
        if (softValue < 10) {
            stringBuffer.append("00").append(softValue);
        } else if (softValue < 100) {
            stringBuffer.append("0").append(softValue);
        } else {
            stringBuffer.append(softValue);
        }
        stringBuffer.append("3F");
        if (mWifiSocketClient != null) {
            mWifiSocketClient.TCPSend(stringBuffer.toString());
        }
    }

    /**
     * Desc:M键按下
     * <p>
     * Author: [李豫]
     * Date: 2020-09-02
     */
    public void onMPress(int mediumValue) {
        mediumValue = (int) (mediumValue * MainActivity.PERCENT);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("TF");
        if (mediumValue < 10) {
            stringBuffer.append("00").append(mediumValue);
        } else if (mediumValue < 100) {
            stringBuffer.append("0").append(mediumValue);
        } else {
            stringBuffer.append(mediumValue);
        }
        stringBuffer.append("4F");
        if (mWifiSocketClient != null) {
            mWifiSocketClient.TCPSend(stringBuffer.toString());
        }
    }

    /**
     * Desc:F键按下
     * <p>
     * Author: [李豫]
     * Date: 2020-09-02
     */
    public void onFPress(int firmValue) {
        firmValue = (int) (firmValue * MainActivity.PERCENT);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("TF");
        if (firmValue < 10) {
            stringBuffer.append("00").append(firmValue);
        } else if (firmValue < 100) {
            stringBuffer.append("0").append(firmValue);
        } else {
            stringBuffer.append(firmValue);
        }
        stringBuffer.append("5F");
        if (mWifiSocketClient != null) {
            mWifiSocketClient.TCPSend(stringBuffer.toString());
        }
    }

    /**
     * Desc:设置压力值
     * <p>
     * Author: [李豫]
     * Date: 2020-09-02
     */
    public void onSetCompressValue(int value) {
        value = (int) (value * MainActivity.PERCENT);
        if (mWifiSocketClient != null) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("TF");
            if (value < 10) {
                stringBuffer.append("00").append(value);
            } else if (value < 100) {
                stringBuffer.append("0").append(value);
            } else {
                stringBuffer.append(value);
            }
            stringBuffer.append("6F");
            mWifiSocketClient.TCPSend(stringBuffer.toString());
        }
    }

    public void onDestory() {
        if (mWifiSocketClient != null) {
            mWifiSocketClient.onRelease();
        }
    }

    /**
     * Desc:发送登录
     * <p>
     * Author: [李豫]
     * Date: 2020-10-25
     *
     * @param name
     * @param password
     */
    public void doLogin(String name, String password) {
        if (mWifiSocketClient != null) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("PA").append(name).append(buildPwd(password)).append("PF");
            mWifiSocketClient.TCPSend(stringBuffer.toString());
        }
    }

    /**
     * Desc:补全8位密码
     * <p>
     * Author: [李豫]
     * Date: 2020-10-27
     *
     * @param password
     * @return string
     */
    private String buildPwd(String password) {
        if (!TextUtils.isEmpty(password)){
            int length = MAX_LENGHT - password.length();
            StringBuffer sb = new StringBuffer();
            if (length > 0) {
                for (int i = 0; i < length; i++) {
                    sb.append("0");
                }
            }
            sb.append(password);
            return sb.toString();
        }
        return password;
    }

    /**
     * Desc:发送登录
     * <p>
     * Author: [李豫]
     * Date: 2020-10-25
     *
     * @param name
     * @param oldPwd
     */
    public void modifyPassword(String name, String oldPwd, String newPwd) {
        if (mWifiSocketClient != null) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("PB").append(name).append(buildPwd(oldPwd)).append("-").append(buildPwd(newPwd)).append("PF");
            mWifiSocketClient.TCPSend(stringBuffer.toString());
        }
    }

    public interface OnWiFiSocketListener {
        void onRespnse(int code, float value);

        void loginSucc();

        void loginFail();

        void modifyPwdSucc();

        void modifyPwdFail();
    }
}
